<?php
class GroupsController extends AppController{
	
	function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allowedActions = array('index', 'view');
	}
	
	function index() {
		$liste_des_groupes = $this->Group->find('all',array(
                    'recursive' => -1
                    )
                );
		
		
		$nbrows = $this->Group->getNumRows();
		
		$result = array (
			'total' => $nbrows,
			'results' => $liste_des_groupes);
		
		
		$this->set('groups',$result);
		
	}
	
	function view(){
		
	}
	function getNode($id_parent = null){                
		$this->set('node',$this->getChilds($id_parent));
	
	}
	
	// Fonction récursive renvoyant tous les descendants d'un noeud d'un arbre
	function getChilds($id_parent = null){
		$noeuds_extraits = $this->Group->find('all', array(
		'conditions' => array('parent_id' => $id_parent),
		'fields' => array('id', 'name'),
		'recursive'=> -1,
		'order' => 'name DESC'));
	
		$noeuds = array();
		
		$n = 0;

		foreach($noeuds_extraits as $cle => $valeur){
			
				$children = $this->getChilds($valeur['Group']['id']);
				if ($children == null){
					$cond = 'true';
				}  else {
					$cond = 'false';
				}
					
			

				$noeud = array(
						'text' => $valeur['Group']['name'],
						'id' => $valeur['Group']['id'], 
						'leaf' => $cond,
						'iconCls' => 'icon-group',
						'children' => $children);

				$noeuds[$n] = $noeud ;
				$n++;
		}
		
		return $noeuds;

		
	}
	
	
}
?>